<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SessionsController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest',[
            'only'=>[
                'create'
            ]
        ]);
    }

    //
    public function create()
    {
        return view('sessions.create');
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'email' => 'required|email|max:255',
            'password' => 'required'
        ]);

        $credentials = [
            'email'    => $request->email,
            'password' => $request->password,
        ];

        if (Auth::attempt($credentials,$request->has('remember'))) {
            if (Auth::user()->activated){
                return redirect()->intended(route('users.show',Auth::user()))->with('success', '欢迎回来！');
            }else{
                Auth::logout();
                return redirect()->back()->withInput()->with('warning', '你的账号未激活，请检查邮箱中的注册邮件进行激活。');
            }
        } else {
            return redirect()->back()->withInput()->with('danger', '很抱歉，您的邮箱和密码不匹配');
        }

    }

    public function destroy()
    {

        Auth::logout();
        return redirect()->route('login')->with('success', '您已成功退出！');
    }




}
